package com.example.thegreatwall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.thegreatwall.common.R;
import com.example.thegreatwall.common.api.CommonResult;
import com.example.thegreatwall.entity.ProductDetail;
import com.example.thegreatwall.mapper.ProductDetailMapper;
import com.example.thegreatwall.service.ProductDetailService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/productdetail")
@CrossOrigin(origins = "*") //无论什么源都可以访问  不写这个不允许跨域
@RestController
public class ProductDetailController{
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private ProductDetailService productDetailService;

    /**
     * 查询产品信息
     * @param pid 产品id
     * @param ptype 产品类型 0-单个产品 1-组合套餐
     * @return
     */
    @GetMapping("/getProductInfo")
    public R getTProductInfo(@Param("id") Integer pid,Integer ptype){
        QueryWrapper<ProductDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id",pid).and(i->i.eq("product_type",ptype)).and(i->i.eq("status",1)).orderByDesc("pd_price");;
        List<ProductDetail> list = productDetailMapper.selectList(queryWrapper);
        return new R().addDate("productInfo",list);
    }

    /**
     * 获取商品的所有规格详情
     * 返回某个商品下的所有可选规格，如不同时段的票价、不同类型的套餐等
     *
     * @param productId 商品ID
     * @param pageNum 当前页码，默认1
     * @param pageSize 每页数量，默认10
     * @return 返回分页的商品规格详情列表
     */
    @GetMapping("/list/{productId}")
    public CommonResult<Page<ProductDetail>> getDetailsByProductId(
            @PathVariable Integer productId,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<ProductDetail> details = productDetailService.getDetailsByProductId(productId, pageNum, pageSize);
        return CommonResult.success(details);
    }

    /**
     * 查看产品对象
     * @param product_detail_id
     * @return
     */
    @GetMapping(value = "/pro")
    public R pro(@Param("product_detail_id")Integer product_detail_id){
        QueryWrapper<ProductDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_detail_id",product_detail_id);
        ProductDetail one = productDetailService.getOne(queryWrapper);
        return new R().addDate("pro",one);
    }

    /**
     * 获取单个规格详情
     * 获取某个具体规格的详细信息，如具体价格、库存、游玩时间等
     *
     * @param productDetailId 规格详情ID
     * @return 返回单个商品规格的详细信息
     */
    @GetMapping("/{productDetailId}")
    public CommonResult<ProductDetail> getDetail(@PathVariable Integer productDetailId) {
        ProductDetail detail = productDetailService.getDetail(productDetailId);
        return CommonResult.success(detail);
    }

    /**
     * 获取可用的规格详情
     * 获取某个商品当前可购买的规格详情，会检查库存和状态
     * 支持按游玩时间筛选，如果不指定时间则返回所有可用时段
     *
     * @param productId 商品ID
     * @param playtime 游玩时间，可选参数，格式为 HH:mm:ss
     * @param pageNum 当前页码，默认1
     * @param pageSize 每页数量，默认10
     * @return 返回分页的可用商品规格列表
     */
    @GetMapping("/available/{productId}")
    public CommonResult<Page<ProductDetail>> getAvailableDetails(
            @PathVariable Integer productId,
            @RequestParam(value = "playtime", required = false) String playtime,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        Page<ProductDetail> details = productDetailService.getAvailableDetails(productId, playtime, pageNum, pageSize);
        return CommonResult.success(details);
    }

    @GetMapping(value = "timesss")
    Map<String, Object> timesss(@RequestParam Integer product_id){
        return productDetailService.timesss(product_id);
    }


    @GetMapping(value = "orderdetail")
    public R orderdetail(@RequestParam Integer detail_id,@RequestParam Integer user_id){
        List<ProductDetail> productDetail = productDetailService.getProductDetail(detail_id, user_id);
        return new R().addDate("orderdetail",productDetail);
    }

    @GetMapping(value = "selidandtype")
    public ProductDetail selidandtype(@RequestParam Integer product_detail_id){
        ProductDetail one = productDetailService.getOne(new QueryWrapper<ProductDetail>().eq("product_detail_id",product_detail_id));
        return one;
    }
}